from keyboard import wait, add_hotkey
from json import dump
from time import time, sleep


swapPath = '../swap.json'
obj = {
    "filepath": "",
    "marks": []
}
start = time()


def add_good_mark():
    t = time() - start
    obj['marks'].append({
        'label': 'good',
        'start': t,
        'end': t
    })
    print(f'GOOD mark added on about {t:.2f} sec')


def add_bad_mark():
    t = time() - start
    obj['marks'].append({
        'label': 'bad',
        'start': t,
        'end': t
    })
    print(f'BAD  mark added on about {t:.2f} sec')


def main():
    print(
        '''Program started.
        
        press UP for good mark
        press DOWN for bad mark
        press SPACE to stop

        '''
    )
    add_hotkey('up', add_good_mark)
    add_hotkey('down', add_bad_mark)
    wait('space')
    print('recording ends')
    with open(swapPath, 'w') as file:
        dump(obj, file)
    print('written to file successfully. exiting in 3s.')
    sleep(3)


if __name__ == '__main__':
    main()
